<?php 
	$order_data = $_SESSION['p_order'] ? $_SESSION['p_order']  : array();
 
	if($order_data){
		$bbuy = '<input type="submit" name="buy" value="buy" />';
	}

	$tp->Block('list');
	$count =0;
	$p_totle=0;
	$p_product = array();
	
	$p_discount_total = 0;
	$discount = new discount;
	
	foreach($order_data as $key => $res){
		$sql = "SELECT * FROM `v_product` WHERE `product_id` = '".$res['product_id']."' AND `status` = '1' LIMIT 1";
		$query = mysql_query($sql);
		while($result = mysql_fetch_assoc($query)) {
			$p_title	= $result['title'];
			$p_url		= $result['url'];
			$p_brand_url= $result['brand_url'];
			$p_brand_thumb= $result['brand_thumb'] ?  $result['brand_thumb']: "";
			$p_thumb	= $result['thumb'] ?  $result['thumb']: "";
 
			$p_price	= $result['price'];
			$p_totle	= $p_totle + $p_price;
			$p_totle	= $p_totle;
			$p_price	= number_format($p_price,2);
			
			extract(select('tbl_color','color_id',$res['color'],""), EXTR_PREFIX_ALL , "c");
			extract(select('tbl_size','size_id',$res['size'],""), EXTR_PREFIX_ALL , "s");

			$p_color	= $c_title;
			$p_size		= $s_title;
			$p_product[]= $result['product_id'];
			
			$product_info = array('brand_id'=>$result['brand_id'],'product_id'=>$result['product_id'],'color_id'=>0,'size_id'=>0,'season_id'=>0); // เมื่อหน้านี่ทำใหม่ จะมีทุกสี  
			$discount->set_item($product_info);
			$p_discount = $discount->get_discount_pricce();
			
			$p_discount_total += $p_discount;
			//echo $discount->get_discount_pricce();
			$p_discount = number_format($p_discount,2);
			$count++;
			$tp->Apply();
		}
		mysql_free_result($query);
	}
	$totle	= number_format($p_totle,2);
	$totle_price	= number_format($p_totle-$p_discount_total,2);
	$p_discount_total = number_format($p_discount_total,2);
	$orderid = '1';
	
 	if($_SERVER["REQUEST_METHOD"]=="POST") {
		acl();
		$orcode = $_POST['orcode'];
		if($order_data){
			if(!product_check($order_data)){
				echo 'fail';
				exit;
			}
			$ip	= getUserIP();
			$product = implode(',',$p_product);
			
			

			$sql="INSERT INTO `tbl_buy_order` (`order_id`, `member_id`, `product`, `count`, `card`, `price_total`, `discount_id`, `discount`, `net`, `ip`, `date`, `ssid`) VALUES (NULL, '$ss_member_id', '$product', '$count', '1', '$p_totle', '$discount_id', '$discount', '$p_totle', '$ip', NOW(), '$ss_id');";
			mysql_query($sql);
			$order_id = mysql_insert_id();
			foreach($order_data as $key => $resi){
				$sql = "SELECT `item_id` , `count` FROM  `tbl_product_item` WHERE `product_id` = '".$resi['product_id']."' AND `color_id` = '".$resi['color']."' AND `size_id` = '".$resi['size']."' LIMIT 1";
			 
				$query = mysql_query($sql) or die($sql);
				while($result = mysql_fetch_assoc($query)) {
					$item_id	= $result['item_id'];
					$count		= (int)$result['count'] - 1;
					$sqlupdate	= "UPDATE `tbl_product_item` SET `count` = '$count' WHERE `item_id` = '$item_id' LIMIT 1;";
					mysql_query($sqlupdate) or die($sqlupdate);
				 
				}
				mysql_free_result($query);

				$sqli="INSERT INTO `tbl_buy_history` (`history_id`, `member_id`, `order_id`, `product_id`,`color_id` , `size_id`, `count`, `card`, `price`,`price_total`, `discount_id`, `discount`, `net`, `ip`, `date`, `ssid`) VALUES (NULL, '$ss_member_id', '$order_id', '".$resi['product_id']."', '".$resi['color']."'  ,'".$resi['size']."', '1', '1', '".$resi['price']."','$p_totle', '$discount_id', '$discount', '$p_totle', '$ip', NOW(), '$ss_id');";
				mysql_query($sqli);
			}
			$order_data =  array();
			$_SESSION['p_order'] = array();
	 
			$tp_mail	= new template(VIEW_ABS . '/shopping/mail-report.html');
			$mail_html	= $tp_mail->generate();

			/*include('libs/phpmailer/class.phpmailer.php');

			$mail = new PHPMailer();
			$mail->CharSet	= "utf-8";
			$mail->From     = "webmaster@ss.com";
			$mail->FromName = "aaa";
			$mail->Subject	= "Report: ";
			$mail->IsHTML(true);
			$mail->Body		= $mail_html;
			$mail->AddAddress($ss_email, $ss_first_name.' '.$ss_last_name);
			$mail->Send();
			$mail->ClearAddresses();
			*/
			header("location: /shopping/thankyou/");
			exit;
		}
		$order_data =  array();
		$_SESSION['p_order'] = array();
		header("location: /shopping/");
		exit;
	}
?>